package com.xuan.yupao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuan.yupao.common.ErrorCode;
import com.xuan.yupao.exception.BusinessException;
import com.xuan.yupao.model.domain.Follow;
import com.xuan.yupao.model.domain.User;
import com.xuan.yupao.model.domain.vo.UserVo;
import com.xuan.yupao.service.FollowService;
import com.xuan.yupao.mapper.FollowMapper;
import com.xuan.yupao.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author 清凉茶
* @description 针对表【follow(关注表)】的数据库操作Service实现
* @createDate 2024-07-08 17:45:29
*/
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow>
    implements FollowService{

    @Resource
    private UserService userService;

    @Override
    public String followOrUnfollowUser(Long userId, Long fanId) {
        // 查看 关注关系表 是否存在记录
        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("fan_id", fanId);
        long count = this.count(queryWrapper);
        boolean result = false;
        if (count > 0) {
            // 删除记录
            result = this.remove(queryWrapper);
        } else {
            // 添加记录
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFanId(fanId);
            result = this.save(follow);
        }
        if (!result) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "操作失败");
        }
        return "ok";
    }

    @Override
    public List<UserVo> getFollowUsers(Long fanId) {
        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("fan_id", fanId);
        List<Long> userIdList = this.list(queryWrapper).stream().map(Follow::getUserId).collect(Collectors.toList());
        // 没有任何关注的用户,直接返回即可
        if (CollectionUtils.isEmpty(userIdList)) {
            return new ArrayList<>();
        }

        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.in("id", userIdList);
        return userService.list(userQueryWrapper).stream().map(user -> {
            UserVo userVo = userService.getSafetyUser(user);
            userVo.setIsFollow(true);
            return userVo;
        }).collect(Collectors.toList());
    }

    @Override
    public List<UserVo> getMyFans(Long userId) {
        QueryWrapper<Follow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<Long> fanIdList = this.list(queryWrapper).stream().map(Follow::getFanId).collect(Collectors.toList());
        // 没有粉丝,直接返回即可
        if (CollectionUtils.isEmpty(fanIdList)) {
            return new ArrayList<>();
        }

        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.in("id", fanIdList);
        return userService.list(userQueryWrapper).stream().map(user -> {
            UserVo userVo = userService.getSafetyUser(user);
            // 查看是否与用户互粉
            QueryWrapper<Follow> followQueryWrapper = new QueryWrapper<>();
            followQueryWrapper.eq("fan_id", userId).eq("user_id", user.getId());
            long count = this.count(followQueryWrapper);
            userVo.setIsFollow(count > 0);
            return userVo;
        }).collect(Collectors.toList());
    }

}




